package service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

import library.service.Book;
import library.service.LibraryService;

public class SciFiLibraryServiceImpl implements LibraryService, ManagedService {

	private Collection books = new ArrayList();

	public SciFiLibraryServiceImpl() {
	}

	// books.add(new SciFiBookImpl("Java"));
	// books.add(new SciFiBookImpl("C#"));
	// books.add(new SciFiBookImpl("Ruby"));

	// --- ManagedService interface implementation -----------------------------

	public void updated(Dictionary properties) throws ConfigurationException {
		System.out.println("ManagedService.updated() method");
		
		/////////
		System.out.println("start test");
		System.out.println("properties.size = "+properties.size());
		Enumeration enum = properties.elements();
		Object obj = enum.nextElement();
		System.out.println("String1: >" + (String)obj +"<");
		
//		try {
			Object obj2 = enum.nextElement();
			System.out.println("String2: >" + (String)obj2 +"<");
		// } catch (RuntimeException re) {
		// re.printStackTrace();
		// }
		
		///////////
		System.out.println("finish test");
		if (properties != null && properties.size() > 0) {

			String[] bookNames = (String[]) properties.get("bookNames");

			if (bookNames != null && bookNames.length > 0) {
				System.out.println("Count of books: " + bookNames.length);
				for (int i = 0; i < bookNames.length; i++) {
					String name = bookNames[i];
					System.out.println("name: " + name);
					Dictionary bookProps = new Hashtable(1);
					bookProps.put("name", name);
					this.addBook(bookProps);
				}
			} else {
				System.out.println("bookNames == null");
			}
		} else {
			System.out.println("Null-pointed or empty 'properties' "
					+ "input argument.");
		}

	}

	// --- LibraryService interface implementation -----------------------------
	public Book addBook(Dictionary bookProperties) {

		if (bookProperties == null || bookProperties.isEmpty()) {
			throw new IllegalArgumentException(
					"Null-pointed or empty bookProperties dictionary.");
		}

		Book book = null;

		if (bookProperties != null && !bookProperties.isEmpty()) {

			String bookName = (String) bookProperties.get("name");
			if (bookName != null) {
				book = new SciFiBookImpl(bookName);
			} else {
				throw new IllegalArgumentException(
						"No 'name' property found for new added book.");
			}
		} else {
			throw new IllegalArgumentException(
					"Null-pointed or empty bookProperties input argument.");

		}

		if (books.contains(book)) {
			throw new IllegalArgumentException(
					"The book is already in the library.");
		}

		books.add(book);
		return book;
	}

	public void removeBook(Book book) {
		if (book == null) {
			throw new IllegalArgumentException("Null-pointed input book");
		}

		boolean deletionResult = books.remove(book);
		if (deletionResult) {
			System.out.println("Book with name " + book.getName()
					+ " is removed.");
		}

	}

	public Book findBookByName(String bookName) {
		if (bookName == null) {
			throw new IllegalArgumentException("illegal input");
		}

		// TODO optimize using Factory and Collections.contains()

		Iterator iterator = books.iterator();
		while (iterator.hasNext()) {
			Book book = (Book) iterator.next();
			if (book.getName().equals(bookName)) {
				return book;
			}
		}
		return null;
	}

	public Book[] listBooks() {
		Book[] bookArray = new Book[books.size()];
		bookArray = (Book[]) books.toArray(bookArray);
		return bookArray;
	}
}
